*******************
***** Table 6 *****
*******************
global Section4 "D:\Dropbox\unequal_gains\QJE revision plan\analysis\section4_data"
global resultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results"


* 1. Panel A. Stability across product space fixed effects 
use "$Section4/analysis_main.dta", clear

* prepare table
gen Description=" "
replace Description="Beta" if [_n]==1
replace Description="SE" if [_n]==2
replace Description="First Stage F" if [_n]==3
* note first stage F stat will be adjusted for clustering across product modules
replace Description="Cragg-Donald F" if [_n]==4
replace Description="N" if [_n]==5

* A. CES for continued goods

* baseline from Table 6
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending=inst_linageres_w) [aw=sqrt(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_1=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_1=`b' if Description=="Beta"
replace Col_1=`se' if Description=="SE"
replace Col_1=e(rkf) if Description=="First Stage F"
replace Col_1=e(cdf) if Description=="Cragg-Donald F"
replace Col_1=e(N) if Description=="N"

* quality f.e.
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending_w=inst_linageres_w) i.quality [aw=log(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_2=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_2=`b' if Description=="Beta"
replace Col_2=`se' if Description=="SE"
replace Col_2=e(rkf) if Description=="First Stage F"
replace Col_2=e(cdf) if Description=="Cragg-Donald F"
replace Col_2=e(N) if Description=="N"

* department f.e.
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending_w=inst_linageres_w) i.department_code [aw=sqrt(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_3=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_3=`b' if Description=="Beta"
replace Col_3=`se' if Description=="SE"
replace Col_3=e(rkf) if Description=="First Stage F"
replace Col_3=e(cdf) if Description=="Cragg-Donald F"
replace Col_3=e(N) if Description=="N"

* product group f.e.
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending_w=inst_linageres_w) i.product_group_code [aw=log(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_4=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_4=`b' if Description=="Beta"
replace Col_4=`se' if Description=="SE"
replace Col_4=e(rkf) if Description=="First Stage F"
replace Col_4=e(cdf) if Description=="Cragg-Donald F"
replace Col_4=e(N) if Description=="N"

* B. Full inflation

* baseline from Table 6
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending=inst_linageres_w) [aw=sqrt(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_5=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_5=`b' if Description=="Beta"
replace Col_5=`se' if Description=="SE"
replace Col_5=e(rkf) if Description=="First Stage F"
replace Col_5=e(cdf) if Description=="Cragg-Donald F"
replace Col_5=e(N) if Description=="N"


* quality f.e.
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending_w=inst_linageres_w) i.quality [aw=log(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_6=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_6=`b' if Description=="Beta"
replace Col_6=`se' if Description=="SE"
replace Col_6=e(rkf) if Description=="First Stage F"
replace Col_6=e(cdf) if Description=="Cragg-Donald F"
replace Col_6=e(N) if Description=="N"


* department f.e.
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending_w=inst_linageres_w) i.department_code [aw=sqrt(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_7=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_7=`b' if Description=="Beta"
replace Col_7=`se' if Description=="SE"
replace Col_7=e(rkf) if Description=="First Stage F"
replace Col_7=e(cdf) if Description=="Cragg-Donald F"
replace Col_7=e(N) if Description=="N"


* product group f.e.
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending_w=inst_linageres_w) i.product_group_code [aw=log(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_8=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_8=`b' if Description=="Beta"
replace Col_8=`se' if Description=="SE"
replace Col_8=e(rkf) if Description=="First Stage F"
replace Col_8=e(cdf) if Description=="Cragg-Donald F"
replace Col_8=e(N) if Description=="N"

keep Descr Col*
order Descr Col*
format Col* %6.3f
drop if missing(Col_1)
save "$resultspath/Table6a", replace

* 2. Panel B. Stability across household space fixed effects 

use "$Section4/analysis_main.dta", clear

* prepare table
gen Description=" "
replace Description="Beta" if [_n]==1
replace Description="SE" if [_n]==2
replace Description="First Stage F" if [_n]==3
* note first stage F stat will be adjusted for clustering across product modules
replace Description="Cragg-Donald F" if [_n]==4
replace Description="N" if [_n]==5

* A. CES for continued goods

* (1) age f.e.
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending=inst_ageres_w) [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_1=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_1=`b' if Description=="Beta"
replace Col_1=`se' if Description=="SE"
replace Col_1=e(rkf) if Description=="First Stage F"
replace Col_1=e(cdf) if Description=="Cragg-Donald F"
replace Col_1=e(N) if Description=="N"

* (2) educ
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending_w=inst_educres_w) [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_2=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_2=`b' if Description=="Beta"
replace Col_2=`se' if Description=="SE"
replace Col_2=e(rkf) if Description=="First Stage F"
replace Col_2=e(cdf) if Description=="Cragg-Donald F"
replace Col_2=e(N) if Description=="N"

* (3) raw with dept-quality f.e. 
ivreg2 RMS_lces_price_index_win_w (RMS_avg_Dlog_realspending_w=inst_raw_w) i.department_code i.quality [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_3=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_3=`b' if Description=="Beta"
replace Col_3=`se' if Description=="SE"
replace Col_3=e(rkf) if Description=="First Stage F"
replace Col_3=e(cdf) if Description=="Cragg-Donald F"
replace Col_3=e(N) if Description=="N"

* (4) educ with dept-quality f.e.
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending_w=inst_educres_w) i.department_code i.quality [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_4=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_4=`b' if Description=="Beta"
replace Col_4=`se' if Description=="SE"
replace Col_4=e(rkf) if Description=="First Stage F"
replace Col_4=e(cdf) if Description=="Cragg-Donald F"
replace Col_4=e(N) if Description=="N"

* (5) state with dept-quality f.e.
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending=inst_stateres_w) ///
i.department_code i.quality [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_5=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_5=`b' if Description=="Beta"
replace Col_5=`se' if Description=="SE"
replace Col_5=e(rkf) if Description=="First Stage F"
replace Col_5=e(cdf) if Description=="Cragg-Donald F"
replace Col_5=e(N) if Description=="N"

* (6) children with dept-quality f.e.
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending=inst_raceres_w) ///
i.department_code i.quality [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_6=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_6=`b' if Description=="Beta"
replace Col_6=`se' if Description=="SE"
replace Col_6=e(rkf) if Description=="First Stage F"
replace Col_6=e(cdf) if Description=="Cragg-Donald F"
replace Col_6=e(N) if Description=="N"

* (7) race with dept-quality f.e.
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending=inst_childrenres_w) ///
i.department_code i.quality [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_7=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_7=`b' if Description=="Beta"
replace Col_7=`se' if Description=="SE"
replace Col_7=e(rkf) if Description=="First Stage F"
replace Col_7=e(cdf) if Description=="Cragg-Donald F"
replace Col_7=e(N) if Description=="N"

* B. Full inflation

* (8) age f.e.
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending=inst_ageres_w) [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_8=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_8=`b' if Description=="Beta"
replace Col_8=`se' if Description=="SE"
replace Col_8=e(rkf) if Description=="First Stage F"
replace Col_8=e(cdf) if Description=="Cragg-Donald F"
replace Col_8=e(N) if Description=="N"

* (9) educ
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending_w=inst_educres_w) [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_9=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_9=`b' if Description=="Beta"
replace Col_9=`se' if Description=="SE"
replace Col_9=e(rkf) if Description=="First Stage F"
replace Col_9=e(cdf) if Description=="Cragg-Donald F"
replace Col_9=e(N) if Description=="N"

* (10) raw with dept-quality f.e. 
ivreg2 RMS_lfullces_pi_win_common_w (RMS_avg_Dlog_realspending_w=inst_raw_w) i.department_code i.quality [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_10=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_10=`b' if Description=="Beta"
replace Col_10=`se' if Description=="SE"
replace Col_10=e(rkf) if Description=="First Stage F"
replace Col_10=e(cdf) if Description=="Cragg-Donald F"
replace Col_10=e(N) if Description=="N"

* (11) educ with dept-quality f.e.
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending_w=inst_educres_w) i.department_code i.quality [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_11=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_11=`b' if Description=="Beta"
replace Col_11=`se' if Description=="SE"
replace Col_11=e(rkf) if Description=="First Stage F"
replace Col_11=e(cdf) if Description=="Cragg-Donald F"
replace Col_11=e(N) if Description=="N"

* (12) state with dept-quality f.e.
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending=inst_stateres_w) ///
i.department_code i.quality [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_12=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_12=`b' if Description=="Beta"
replace Col_12=`se' if Description=="SE"
replace Col_12=e(rkf) if Description=="First Stage F"
replace Col_12=e(cdf) if Description=="Cragg-Donald F"
replace Col_12=e(N) if Description=="N"

* (13) race with dept-quality f.e.
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending=inst_raceres_w) ///
i.department_code i.quality [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_13=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_13=`b' if Description=="Beta"
replace Col_13=`se' if Description=="SE"
replace Col_13=e(rkf) if Description=="First Stage F"
replace Col_13=e(cdf) if Description=="Cragg-Donald F"
replace Col_13=e(N) if Description=="N"

* (14) children with dept-quality f.e.
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending=inst_childrenres_w) ///
i.department_code i.quality [aw=log(RMS_avg_spending)], first cluster(product_module_code)
gen Col_14=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_14=`b' if Description=="Beta"
replace Col_14=`se' if Description=="SE"
replace Col_14=e(rkf) if Description=="First Stage F"
replace Col_14=e(cdf) if Description=="Cragg-Donald F"
replace Col_14=e(N) if Description=="N"

* Finalize table and save
keep Descr Col*
order Descr Col*
format Col* %6.3f
drop if missing(Col_1)
save "$resultspath\Table6b", replace

